<div class="bs-callout bs-callout-warning">
    <h4>Nếu bạn có bất cứ câu hỏi nào, hãy cùng chia sẻ để được giải đáp</h4>
    <?php if($this->session->userdata('account')) { ?>
    <form id="form" class="form-horizontal" role="form" method="POST">
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">Tiêu đề</label>
            <div class="col-sm-8">
                <input type="text" class="form-control title" id="title" placeholder="Tiêu đề" name="title" required="true" />
            </div>
            <div class="col-sm-2">
                <button type="submit" class="btn btn-primary" id="submit">Gửi</button>
            </div>
        </div>
        <div class="form-group">
            <label for="question" class="col-sm-2 control-label">Nội dung chi tiết</label>
            <div class="col-sm-8">
                <textarea class="form-control content" placeholder="Nội dung chi tiêt" name="question" required="true" ></textarea>
            </div>
        </div>
    </form>
    <?php } else { ?>
    <div style="text-align: right;"><a href="<?php echo base_url().'login'?>">Đăng nhập đặt câu hỏi</a></div>
    <?php } ?>
</div>

<div class="all-questions">
    <?php
        foreach($questions as $k=>$v) {
    ?>
    <div class="bs-callout bs-callout-danger">
        <quote>
        <?php echo $v['title']?>
        <cite>
            <?php echo $v['question']?><br />
            <p style="text-align: right;">Đăng bởi: <?php echo $v['accountid']?> vào lúc <?php echo $v['startdate']?></p>
        
        
        <?php if($v['answer']) { ?>
            <div>Trả lời bạn đọc:</div>
            <?php echo $v['answer']?>
            <p style="text-align: right;">Trả lời: <?php echo $v['answerid']?> vào lúc <?php echo $v['enddate']?></p>
        <?php } ?>
        </cite>
        </quote>
    </div>
    
    <div class="bs-callout bs-callout-info">
        <h4></h4>
        <?php if($this->session->userdata('account')) { ?>
        <form id="form<?php echo $v['id']?>" class="form-horizontal" role="form" method="POST">
            <div class="form-group">
                <div class="col-sm-2"></div>
                <div class="col-sm-8">
                    <textarea class="form-control" placeholder="Lời bình" name="content" required="true" ></textarea>
                </div>
                <div class="col-sm-2">
                    <input type="hidden" name="questionid" value="<?php echo $v['id']?>"/>
                    <button type="submit" class="btn btn-primary" id="submit<?php echo $v['id']?>">Gửi</button>
                </div>
            </div>
        </form>
        <?php } else { ?>
        <div style="text-align: right;"><a href="<?php echo base_url().'login'?>">Đăng nhập để gửi bình luận</a></div>
        <?php } ?>
        <div class="comment-block-<?php echo $v['id']?>">
            <?php
            $comments = $v['comments'];
            foreach($comments as $key=>$value) {
            ?>
                <blockquote class="pull-right" style="text-align: right; width: 100%;">
                    <h6><?php echo $value['content']?></h6>
                    <small>Đăng bởi <?php echo $value['accountid']?> vào lúc <?php echo date('d-m-Y H:i:s', strtotime($value['startdate']))?></small>
                </blockquote>
            <?php
                }
            ?>
            <div class="clearfix"></div>
        </div>
    </div>
    
    <?php } ?>
</div>

<script src="http://js.pusher.com/2.1/pusher.min.js"></script>

<?php foreach($questions as $k=>$v) { ?>
    <script>
        $(function(){
        	var pusher = new Pusher('4a714ccbd52501e37d40');
        	var channel<?php echo $v['id'];?> = pusher.subscribe('comments<?php echo $v['id'];?>');
        	channel<?php echo $v['id'];?>.bind('comment', function(data){
        		var html = '<blockquote class="pull-right last" style="text-align: right; width: 100%;"><h6>'+data.comment+'</h6><small>Đăng bởi '+data.name+' vào lúc '+data.date+'</small></blockquote>';
        		var comments = $('.comment-block-<?php echo $v['id'];?>');
        		comments.prepend(html);
        		comments.find('.last').slideDown().removeClass('last');
        	});
        	
        	$('#form<?php echo $v['id'];?>').on('submit', function(){
        		var data = $(this).serialize();
        		$.post('<?php echo base_url().'ajax/postcommentquestion'?>', data, function(){
        			$('#form<?php echo $v['id'];?>').trigger('reset');
        			return true;
        		});
        		return false;
        	});
        });
    </script>
    
<?php }?>